﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Invoicing.AJAX
{
    /// <summary>
    /// SysDeptInfo 的摘要说明
    /// </summary>
    public class SysDeptInfo : IHttpHandler
    {
        private HttpRequest Req = HttpContext.Current.Request;
        private ZY.BLL.Tab_DeptInfo dal = new ZY.BLL.Tab_DeptInfo();

        private string _pageNum = "1";

        public string PageNum
        {
            get { return _pageNum; }
            set { _pageNum = value; }
        }
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            //得到当前页的页码
            if (Req["pageNum"] != null)
            {
                PageNum = Req["pageNum"].ToString();
            }

            //选择操作类型
            if (Req["action"]!=null)
            {
                switch (Req["action"].ToString())
                {   
                    case "add": //添加

                        try
                        {
                            ZY.Model.Tab_DeptInfo obj=new ZY.Model.Tab_DeptInfo();
                            #region 获取请求参数

                            obj.dp_lastime = DateTime.Now;
                            obj.dp_leve = int.Parse(Req["dp_level"].ToString());
                            obj.dp_namme = Req["txtDept"].ToString();
                            obj.dp_note = Req["txtDeptinfo"].ToString();
                            obj.dp_createtime = DateTime.Now;
                                
                            obj.dp_owner = int.Parse(Req["txtOwer"].ToString());

                            dal.Add(obj);
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "添加成功!", "deptlist", "sys/DeptInfo.aspx"));

                            #endregion

                        }
                        catch (Exception ex)
                        {

                           context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message, "deptlist", "sys/DeptInfo.aspx"));
                          //throw ex;
                        }
                  
                        break;
                    case "edit"://编辑

                        #region 编辑代码
                        //获得请求参数类型和对象ID
                        int did = 0;
                        if (Req["did"] != null)
                        {
                            did = int.Parse(Req["did"].ToString());
                            ZY.Model.Tab_DeptInfo temp = dal.GetModel(did);
                            #region 开始获取其他的请求参数

                            temp.dp_namme = Req["txtDept"].ToString();
                            temp.dp_owner = int.Parse(Req["txtOwer"].ToString());
                            temp.dp_leve = int.Parse(Req["dp_level"].ToString());
                            temp.dp_note = Req["txtDeptinfo"].ToString();
                            temp.dp_lastime = DateTime.Now;

                            #endregion
                            //执行修改
                            try
                            {
                                dal.Update(temp);
                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "操作成功", "deptlist", "sys/DeptInfo.aspx?pageNum=" + PageNum));
                            }
                            catch (Exception ex)
                            {
                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message, "deptlist", "sys/DeptInfo.aspx?pageNum=" + PageNum));
                            }
                        }
                        #endregion 

                        break;
                    case "del": //删除

                        #region 删除对象数据
                       // int dtemp = int.Parse(Req["did"].ToString());

                        string ids = Req["ids"].ToString();

                        //ZY.Model.Tab_DeptInfo dedit = dal.GetModel(dtemp);

                        string[] param = ids.Split(',');

                        foreach (var item in param)
                        {

                            try
                            {
                                if (item != null)
                                {

                                    dal.Delete(int.Parse(item));
                                  

                                }
                            }
                            catch (Exception ex)
                            {

                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message, "deptlist", "sys/DeptInfo.aspx?pageNum=" + PageNum));
                            }

                        }

                        context.Response.Write(Pubs.AjaxHandle.AjaxPressTodo("200", "删除成功"));
                     
                 
                        #endregion 
                        break;
                    default:
                        break;
                }
            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}